﻿// JScript 文件

Ext.data.AjaxProxy = function(ajaxProObject, method) {
    Ext.data.AjaxProxy.superclass.constructor.call(this);
    this.ajaxProObject = ajaxProObject;
    this.method = method;
};

Ext.extend(Ext.data.AjaxProxy, Ext.data.DataProxy, {
    load: function(params, reader, callback, scope, arg) {
        
        if(this.fireEvent("beforeload", this, params) !== false) {
            var s = [];
            for (var x in params) {
                s[s.length] = "params[\"" + x + "\"]";
            }
            s = s.join(",");
            var o = {
                    params: params || {},
                    request: {
                        callback : callback,
                        scope : scope,
                        arg : arg
                    },
                    reader: reader,
                    callback: this.loadResponse,
                    scope: this
                };
            
            // old
            //eval("this.ajaxProObject[this.method](" + s + ", this.loadResponse, o)");
            
            //pjf modify in 2008-10-29
            if(s=="")
            {
                eval("this.ajaxProObject[this.method](this.loadResponse, o)");
            }
            else
            {
                eval("this.ajaxProObject[this.method](" + s + ", this.loadResponse, o)");
            }
            
            
        } else {
            callback.call(scope||this, null, arg, false);
        }
    },
    
    loadResponse: function(response, request) {
        var o = response.context;
        var self = o.scope;
        if(!!response.error) {
            self.fireEvent("loadexception", self, o, response.error);
            o.request.callback.call(o.request.scope, null, o.request.arg, false);
            return;
        }
        var result;
        var json = {responseText: response.json.substring(0, response.json.length - 3)};
        try {
            result = o.reader.read(json);
        } catch(e) {
            self.fireEvent("loadexception", self, o, json, e);
            o.request.callback.call(o.request.scope, null, o.request.arg, false);
            return;
        }
        o.request.callback.call(o.request.scope, result, o.request.arg, true);
    }
});
